﻿using System;

namespace FluentParser.Expressions.Terminals.Parsing
{
    /// <summary>
    /// This symbol is successful only if the cursor is positionned
    /// exactly at the end of the text.
    /// </summary>
    /// <example><code>
    /// var end = EndOfText;
    /// 
    /// end.Parse("")            --> success
    /// end.Parse("SomeText")    --> failure
    /// end.Parse("SomeText", 8) --> success
    /// </code></example>
    public class EndOfTextSymbolTerminal : Symbol
    {
        public EndOfTextSymbolTerminal()
        {
            TypeOperation = OperationType.Parsing;
        }

        public override ParsingResult<object> Parse(GrammarContext context, int index)
        {
            if (index == context.Text.Length)
                return new ParsingResult<object>(index, 0, String.Empty);

            return null;
        }

        public override void ParseNode(GrammarContext context, ParsingNodeResult node)
        {
            node.ExploredLength = 1;

            if (node.Index == context.Text.Length)
            {
                node.IsSuccess = true;
                node.Value = String.Empty;
            }
        }
    }
}